<template>
	<view class="exchange-card-box">
		<view class="card-box">
			<view class="card-title">卡密兑换</view>
			<view class="card-input"><input class="uni-input" placeholder="请输入卡号" :value="card" @input="onInputTo" /></view>
			<view class="card-input"><input class="uni-input" placeholder="请输入卡密" :value="cardNum" @input="onInput" /></view>
			<view class="exchange-btn">
				<button type="primary" @click="exchange()">立即兑换</button>
			</view>
			<view class="exchange-list" @click="gotoexchange()">我的兑换列表<text class="card-more iconfont iconright"></text></view>
		</view>	
	</view>
</template>

<script>
	export default {
		data() {
			return {	
				cardNum: '',
				card : ''
			};
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
		},
		methods: {
			onInput(e) {
				const { value } = e.target;
				this.cardNum = value
			},
			onInputTo(e){
				const { value } = e.target;
				this.card = value
			},
			gotoexchange() {
				this.$util.redirectTo('/promotionpages/giftcard/my_exchange/exchange_list');
			},
			exchange() {
				this.$api.sendRequest({
					url:'/giftcard/api/giftcard/exchangeInfo',
						data: {
							carmichael_text: this.cardNum,
							entity_bianhao : this.card
						},
					success: res => {
						if(res.code >= 0){
							if(res.data){
								if(res.data.entity_status == 3 || res.data.entity_status == 4 || res.data.entity_status == 2){
									this.$util.showToast({
										title: "对不起，该卡已被使用或作废，请更换其他礼品卡"
									});
									return;
								}
								if(res.data.entity_status == 0){
									this.$util.showToast({
										title: "该礼品卡未激活"
									});
									return;
								}
								if(res.data.goods_list.length){
									
									let giftcard_id = res.data.giftcard_id;
									let id = res.data.id;
									
									let giftcard_sku_id = [];
									res.data.goods_list.forEach(siteItem => {
										giftcard_sku_id.push(siteItem.sku_id)
									});
												
									uni.setStorage({
										key: 'orderCreateData',
										data: {
											sku_id: giftcard_sku_id.toString() ,
											num:1
										},
										success: () => {
											this.$util.redirectTo('/promotionpages/giftcard/payment/payment',{ giftcard_id:giftcard_id , id:id })
										}
									});
								}else{
									this.$api.sendRequest({
										url: '/giftcard/api/giftcard/exchange',
										data: {
											carmichael_text: this.cardNum
										},
										success: res => {
											if (res.code == 0 && res.data) {
												this.$util.showToast({
														title: res.data
													});
												this.cardNum='';
												setTimeout(() => {
													this.$util.redirectTo('/promotionpages/giftcard/my_exchange/exchange_list', {}, 'redirectTo');
												}, 1000);

											} else {
												this.$util.showToast({
													title: res.message
												});
											}
											if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
											
										},
										fail: res => {
											mescroll.endErr();
											if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
										}
									});
								}
							}else{
								this.$util.showToast({
									title: "卡号或密码错误，请重新输入"
								});
							}
						}else{
							this.$util.showToast({
								title: res.message
							});
						}
					}
				})
			},
		},
	};
</script>

<style lang="scss">
	.exchange-card-box{
		//background:#000;
		height: 100vh;
		background-image: url();
		background-size: 100% 100%;
		.card-box{
			background: #fff;
			position: relative;
			top: 320rpx;
			width: 90%;
			margin: 0 auto;
			border-radius: 10rpx;
			padding-bottom: 10px;
			.card-title{
				text-align: center;
				font-size: 32rpx;
				padding: 20rpx 0px;
			}
			.card-input{
				width: 80%;
				margin: 0 auto;
				margin-bottom: 40rpx;
				.uni-input{
					height: 80rpx
				}
			}
		}
	}
	.cate-image{
		display: flex;
		image{
			width: 170rpx;
			height: 116rpx;
			margin:274rpx auto 100rpx auto;
		}
	}
	.cate-search {
		// margin-top: 399rpx;
		width: 100%;
		height: 80rpx;
		padding: 10rpx 30rpx;
		box-sizing: border-box;
		/* #ifdef H5 */
		padding-top: 30rpx;
		/* #endif */

		input {
			font-size: $font-size-base;
			height: 80rpx;
			padding: 15rpx 25rpx 15rpx 30rpx;
			line-height: 60rpx;
			width: calc(100% - 120rpx);
		}

		.search-box {
			width: 100%;
			background: #fff;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 20rpx;
		}
		
		.input-placeholder {
			text-align: center;
		}
	}

	.exchange-btn {
		margin: 100rpx 0 0 0;

	}
	
	.exchange-list {
		color: #808080;
		margin: 20rpx 0 0 460rpx;
		.card-more{
			font-size: 13px;
		}
	}
</style>
